package zcw.com.lib_leet_code;

/**
 * Created by 朱城委 on 2021/8/23.<br><br>
 *
 * 中等：跳跃游戏 II
 */
public class Topic45 {

    public static void main(String[] args) {
        Topic45 instance = new Topic45();

        int[] array = new int[] {2,3,1,1,4};
        System.out.println(instance.jump(array));

        array = new int[] {2,3,0,1,4};
        System.out.println(instance.jump(array));
    }

    public int jump(int[] nums) {
        int maxPosition = 0;
        int step = 0;
        int len = 0;

        for(int i = 0; i < nums.length - 1; i++) {
            maxPosition = Math.max(i + nums[i], maxPosition);
            if(i == len) {
                len = maxPosition;
                step++;
            }
        }

        return step;
    }

    public int jump2(int[] nums) {
        int position = nums.length - 1;
        int count = 0;

        while (position > 0) {
            for(int i = 0; i < nums.length; i++) {
                if(nums[i] + i >= position) {
                    position = i;
                    count++;
                    break;
                }
            }
        }

        return count;
    }
}
